package alt.fr.ma.np.activities;

import alt.fr.ma.np.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;

public class MainActivity extends Activity {

	private final int ID_MENU_ABOUT = 1;
	private final int ID_SUBMENU_CLEARLOGIN = 2;
	private final int ID_MENU_EXIT = 3;	

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d("NORMAL - MainActivity", "Starting MainActivity!");
		Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
				.getDefaultDisplay();
		switch (display.getRotation()) {
		case Surface.ROTATION_0:
			setContentView(R.layout.main);
			break;
		case Surface.ROTATION_90:
			setContentView(R.layout.main_landscape);
			break;
		case Surface.ROTATION_180:
			break;
		case Surface.ROTATION_270:
			break;
		}
	}

	@Override
	public void onBackPressed() {
		finish();
	}

	public void categoryAction(View view) {
		switch (view.getId()) {
		case R.id.homeButtonAgendas:
			startActivity(new Intent(this, AgendaActivity.class));
			break;
		case R.id.homeButtonAstuces:
			startActivity(new Intent(this, TipsActivity.class));
			break;
		case R.id.homeButtonRestos:
			startActivity(new Intent(this, RestoActivity.class));
			break;
		case R.id.homeButtonSorties:
			startActivity(new Intent(this, PartyActivity.class));
			break;
		case R.id.homeButtonSports:
			startActivity(new Intent(this, SportActivity.class));
			break;
		case R.id.homeButtonTrombi:
			startActivity(new Intent(this, FaceBookActivity.class));
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(Menu.NONE, ID_MENU_ABOUT, Menu.NONE, R.string.menuAbout);
		SubMenu sm = menu.addSubMenu(R.string.menuOptions);
	    sm.add(SubMenu.NONE, ID_SUBMENU_CLEARLOGIN, SubMenu.NONE, R.string.menuClearLogin);
		menu.add(Menu.NONE, ID_MENU_EXIT, Menu.NONE, R.string.menuExit);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == ID_SUBMENU_CLEARLOGIN) {
			SharedPreferences preferences = PreferenceManager
					.getDefaultSharedPreferences(this);
			preferences.edit().remove("login").commit();
			preferences.edit().remove("password").commit();
			return true;
		}
		if (item.getItemId() == ID_MENU_EXIT) {
			finish();
			return true;
		}
		return false;
	}
}
